package GameObjects.Ships;

import java.awt.Graphics2D;
import java.util.Comparator;

import GameObjects.InstanceObject;
import GameObjects.Instances.Instances;
import GameObjects.Physics.DeltaPosition;
import GameObjects.Physics.Position;
import GameObjects.Ships.Weapon.Weapon;
import GameObjects.Ships.Weapon.Bullets.Bullet;

public abstract class ShipObject extends InstanceObject 
{
	protected double maxHealth;
	private double health;
	public double Health(){return health;}
	
	public ShipObject(Instances instances, Position position, double health) 
	{
		super(instances, position);
		this.maxHealth = health;
		this.health = health;
	}
	
	/**
	 * Standard collision occurrence for bullets and shields
	 * Note: Most shield types will override this with their own nuances
	 */
	@Override
	protected void collided(Bullet bullet) 
	{
		//if bullet does more damage than there is health then just lower the bullets damage don't kill it
		double extraDamage = damage(bullet.Damage());
		if(extraDamage > 0)
			bullet.lowerDamage(extraDamage);
		else
			bullet.setToRemove();
	}
	
	@Override
	protected void collided(Ship other) 
	{
		
	}

	@Override
	protected void collided(Weapon other) 
	{

	}

	@Override
	public boolean tick() 
	{
		return false;
	}
	
	@Override
	public void render(Graphics2D g)
	{
		if(hasHealth())
			super.render(g);
	}
	
	public boolean hasHealth()
	{
		return health > 0;
	}

	protected double addHealth(double health)
	{
		this.health += health;
		if(this.health > maxHealth)
		{
			double extra = this.health - maxHealth;
			this.health = maxHealth;
			return extra;
		}
		return 0;
	}
	
	protected double damage(double damage)
	{
		double extraDamage = damage - this.health;
		if(extraDamage < 0)
			extraDamage = 0;
		
		this.health -= damage;
		if(this.health < 0)
			this.health = 0;
		
		return extraDamage;
	}
	
	public void move(Position aroundPosition, DeltaPosition dPosition)
	{
		this.position.rotateAround(aroundPosition, dPosition);
	}
	
	public class ShipObjectComparator implements Comparator<ShipObject>
	{
		public int compare(ShipObject objOne, ShipObject objTwo) 
		{
			return 0;
		}
		
	}
}
